package com.zkyc.framework.user.service.impl;

import com.zkyc.framework.common.pojo.Menu;
import com.zkyc.framework.common.pojo.Right;

import java.util.ArrayList;
import java.util.List;
public class MenuTree{
	private List<Menu> menuList = new ArrayList<Menu>();
	public MenuTree(List<Menu> menuList) {
		this.menuList=menuList;
	}

	//建立树形结构
	public List<Menu> buildTree(){
		List<Menu> treeMenus =new  ArrayList<Menu>();
		for(Menu menuNode : getRootNode()) {
			menuNode=buildChildTree(menuNode);
			treeMenus.add(menuNode);
		}
		return treeMenus;
	}

	//递归，建立子树形结构
	public Menu buildChildTree(Menu pNode){
		List<Menu> childMenus =new  ArrayList<Menu>();
		for(Menu menuNode : menuList) {
			if(menuNode.getParentId().equals(pNode.getId())) {
				childMenus.add(buildChildTree(menuNode));
			}
		}
		pNode.setChildrenList(childMenus);
		return pNode;
	}

	//获取根节点(获取所有的父节点)
	public List<Menu> getRootNode() {
		List<Menu> rootMenuLists =new  ArrayList<Menu>();
		for(Menu menuNode : menuList) {
			if(menuNode.getParentId().equals("0")) {
				rootMenuLists.add(menuNode);
			}
		}
		return rootMenuLists;
	}

}